package com.yalantis.taurus.library;

import ohos.agp.components.*;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

public class StickyScrollLinearLayout extends DirectionalLayout implements Component.TouchEventListener {
    private Component mTopView;
    private Component mTabView;
    private Component mContentView;

    private ScrollHelper mScroller;
    private int mTopViewHeight;
    private float mStartY = -1;
    private float moveY = -1;

    public StickyScrollLinearLayout(Context context) {
        this(context, null,null);
    }

    public StickyScrollLinearLayout(Context context, AttrSet attrSet) {
        this(context, attrSet,null);
    }

    public StickyScrollLinearLayout(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init(context, attrSet, styleName);

    }

    private void init(Context context, AttrSet attrSet, String styleName) {
        setOrientation(VERTICAL);
        mScroller = new ScrollHelper();
    }

    public interface StickyScrollInitInterface{
        Component setTopView();
        Component setTabView();
        Component setContentView();
    }

    public void setInitInterface( StickyScrollInitInterface initInterface){
        if(initInterface == null)
            throw new NullPointerException("initInterface can not be null!");
        this.mTopView = initInterface.setTopView();
        if(this.mTopView != null)
            getTopViewHeight();
        this.mTabView = initInterface.setTabView();
        this.mContentView = initInterface.setContentView();
        if(this.mContentView == null)
            return;
        setTouchEventListener(this);
        setTotalHeight();
        postLayout();
    }

    private void getTopViewHeight(){
        if(mTopView == null)
            return;
        mTopViewHeight = mTopView.getHeight();
    }
    private void setTotalHeight(){
        ComponentContainer.LayoutConfig params = mContentView.getLayoutConfig();
        params.height = getHeight() -mTabView.getHeight();
        setComponentSize(getWidth(),mTopView.getHeight()+mTabView.getHeight()+mContentView.getHeight());
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent ev) {
        switch (ev.getAction()){
            case TouchEvent.PRIMARY_POINT_DOWN:
                mStartY =ev.getPointerScreenPosition(0).getY();
                break;
            case TouchEvent.POINT_MOVE:
                moveY =ev.getPointerScreenPosition(0).getY();
                float  deltaY = moveY - mStartY;
                if(deltaY < 0){//向上滑动
                    if(Math.abs(getTranslationY()) < mTopView.getHeight()){
                        if(Math.abs(deltaY) >= mTopView.getHeight()){
                            setTranslationY(-mTopView.getHeight());
                        }else {
                            setTranslationY(deltaY);
                        }
                    }
                }else if( deltaY >0){//向下滑动
                    if(getTranslationY() == 0){
                        return false;
                    }

                    if(Math.abs(getTranslationY()) == mTopView.getHeight()){
                        if(deltaY >= mTopView.getHeight()){
                            setTranslationY(0);
                        }else {
                            setTranslationY(getTranslationY()+deltaY);
                        }
                    }

                    if(Math.abs(getTranslationY()) < mTopView.getHeight()){
                        float distancesY = getTranslationY()+deltaY;
                        if(distancesY >0 ){
                            distancesY =0;
                        }
                        setTranslationY(distancesY);
                    }

                }
                break;
            default:
                break;

        }
        return false;
    }

}
